package com.hyst.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.hyst.common.ResultBean;
import com.hyst.common.ResultPage;
import com.hyst.domain.Admin;
import com.hyst.service.AdminService;

/**
 * @Description:系统用户表controller
 * @author：dy 
 *@Date：2018年08月26日
 */
@RestController
@RequestMapping("admins")
@CrossOrigin(maxAge = 3600, origins = "*")
public class AdminController {
	@Autowired
	private AdminService adminService;
	
	@GetMapping
	public ResultPage<Admin> getProjects(Admin admin) {
		return ResultPage.success(adminService.list(admin));
	}

	@PostMapping
	public ResultBean<Admin> creat(final Admin admin) {
		return ResultBean.success(adminService.insert(admin));
	}

	/**
	 * @Description: 根据主键查询
	 * @param uniqueid
	 * @return ResultBean<Admin>
	 */
	@GetMapping("{uniqueid}")
	public ResultBean<Admin> getByPriKey(@PathVariable("uniqueid") final String uniqueid) {
		return ResultBean.success(adminService.getByPriKey(uniqueid));
	}

	/**
	 * @Description: 根据主键更新全字段
	 * @param admin
	 * @return ResultBean<Admin>
	 */
	@PatchMapping("{uniqueid}")
	public ResultBean<Admin> update(final Admin admin) {
		return ResultBean.success(adminService.update(admin));
	}

	/**
	 * @Description: 根据主键更新不为默认值的字段
	 * @param admin
	 * @return ResultBean<Admin>
	 */
	//@PutMapping("{uniqueid}")
	@PostMapping("{uniqueid}")
	public ResultBean<Admin> updatePatch(@PathVariable("uniqueid") final String uniqueid,
			final Admin admin) {
		return ResultBean.success(adminService.updateSelective(admin));
	}

	/**
	 * @Description: 根据主键删除
	 * @param uniqueid
	 * @return ResultBean<Admin>
	 */
	@DeleteMapping("{uniqueid}")
	public ResultBean<Admin> delete(@PathVariable("uniqueid") final String uniqueid) {
		return ResultBean.success(adminService.delete(uniqueid));
	}
	
	//绑定下拉用
	@GetMapping("select2")
	@ResponseBody
	public List<Map> getSelect2Data() {
		return adminService.getSelect2Data();
	} 

	// 2018-9-25 chenbin: 新增
	@PostMapping("setUID/{uid}")
	public static String getUIDFromSession(@PathVariable("uid") String uid) {
		String uniqueId = "uniqueId";
		try {
			getRequest().getSession().setAttribute("uniqueid", uid);
		} catch (Exception e) {
			
		}
		uniqueId = uid;
		return uniqueId;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		return attrs.getRequest();
	}

}
